<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Poppins', sans-serif;
        }

        html,
        body {
            display: grid;
            height: 100%;
            /* 相关布局 如grid flexbox 中同时沿着块级和内联方向对齐元素 */
            place-items: center;
            text-align: center;
            background: #f2f2f2;
        }

        .container {
            width: 420px;
            background: #fff;
            padding: 20px 30px;
            border-radius: 5px;
            box-shadow: 0 0 15px rgba(0, 0, 0, .2);
        }

        .container header {
            font-size: 24px;
            font-weight: 600;
            line-height: 33px;
        }

        .container form {
            margin: 20px 5px 10px 5px;
        }

        form .field {
            height: 45px;
            width: 100%;
            display: flex;
            position: relative;
        }

        form .field input {
            height: 100%;
            width: 100%;
            border: 1px solid lightgray;
            padding-left: 15px;
            outline: none;
            border-radius: 5px;
            font-size: 17px;
            transition: all .3s;
        }

        /* -----------------focus选中的焦点变换------------------------- */
        form .field input:focus {
            border-color: #27a360;
            box-shadow: inset 0 0 3px #2fd072;
        }

        form .field .showBtn {
            position: absolute;
            right: 10px;
            top: 50%;
            transform: translateY(-50%);
            /* 光标 指针 */
            cursor: pointer;
            font-size: 15px;
            font-weight: 600;
            display: none;
        }

        form .indicator {
            height: 10px;
            display: flex;
            align-items: center;
            /* 项目位于各行之间留有空白的容器内。 */
            justify-content: space-between;
            margin: 10px 0;
            display: none;
        }

        form .indicator span {
            width: 100%;
            height: 100%;
            background: lightgrey;
            border-radius: 5px;
            position: relative;
        }

        form .indicator span.medium {
            margin: 0 3px;
        }

        form .indicator span::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            border-radius: 5px;
        }

        form .indicator span.active.weak::before {
            background: #ff4757;
        }

        form .indicator span.active.medium::before {
            background: orange;
        }

        form .indicator span.active.strong::before {
            background: #23ad5c;
        }

        form .text {
            font-size: 20px;
            font-weight: 500;
            margin-bottom: -10px;
            display: none;
        }

        form .text.weak {
            color: #ff4757;
        }

        form .text.medium {
            color: orange;
        }

        form .text.strong {
            color: #23ad5c;
        }
    </style>
</head>

<body>
    <div class="container">
        <header>Password Strength Indication <br> in HTML CSS & JavaScript</header>
        <form action="#">
            <div class="field">
                <input onkeyup="trigger()" type="password" placeholder="Type password">
                <span class="showBtn">SHOW</span>
            </div>
            <div class="indicator">
                <span class="weak"></span>
                <span class="medium"></span>
                <span class="strong"></span>
            </div>
            <div class="text"></div>
        </form>
    </div>
</body>
<script>
    const indicator = document.querySelector(".indicator");
    const input = document.querySelector("input");
    const weak = document.querySelector(".weak");
    const medium = document.querySelector(".medium");
    const strong = document.querySelector(".strong");
    const text = document.querySelector(".text");
    const showBtn = document.querySelector(".showBtn");
    let regExpWeak = /[a-z]/;
    let regExpMedium = /\d+/;
    let regExpStrong = /.[!,@,#,$,%,^,&,*,(,),_,+,~,-,=]/;

    function trigger() {
        if (input.value != "") {
            indicator.style.display = "block";
            indicator.style.display = "flex";
            if (input.value.length <= 3 && (input.value.match(regExpWeak) || input.value.match(regExpMedium) || input.value.match(regExpStrong))) no = 1;
            if (input.value.length >= 6 && ((input.value.match(regExpWeak) && input.value.match(regExpMedium)) || (input.value.match(regExpMedium) && input.value.match(regExpStrong)) || (input.value.match(regExpWeak)) && (input.value.match(regExpStrong)))) no = 2;
            if (input.value.length >= 6 && input.value.match(regExpWeak) && input.value.match(regExpMedium) && input.value.match(regExpStrong)) no = 3;
            if (no == 1) {
                weak.classList.add("active");
                text.style.display = "block";
                text.textContent = "Your password is too weak";
                text.classList.add("weak");
            }
            if (no == 2) {
                medium.classList.add("active");
                text.textContent = "Your password is medium";
                text.classList.add("medium");
            } else {
                medium.classList.remove("active");
                text.classList.remove("medium");
            }
            if (no == 3) {
                medium.classList.add("active");
                strong.classList.add("active");
                text.textContent = "Your password is strong";
                text.classList.add("strong");
            } else {
                strong.classList.remove("active");
                text.classList.remove("strong");
            }
            showBtn.style.display = "block";
            showBtn.onclick = function () {
                if (input.type === "password") {
                    input.type = "text";
                    showBtn.textContent = "SHOW";
                } else {
                    input.type = "password"
                    showBtn.textContent = "HIDE";
                }
            }
        } else {
            indicator.style.display = "none";
            text.style.display = "none";
            showBtn.style.display = "none";
        }
    }
</script>

</html>